#!/usr/bin/python
# -*- coding: utf-8 -*-  
import re  
import urllib  
import urllib2  
import cookielib  
from bs4 import BeautifulSoup
from mypost import *

import urllib
import httplib2
import json

#创建一个cookie处理对象
cookiejar = cookielib.CookieJar()
#使用build_opener创建urlOpener，使用刚才创建的cookie处理对象作为cookies处理对象
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))

http = httplib2.Http(".cache")

headers = {'Content-type': 'application/json'}
#headers = {'Connection': 'keep-alive'}

path = 'http://127.0.0.1:8080/drupal/rest/user/token.json'
#path = 'http://127.0.0.1:8080/drupal/services/session/token'
response, token= http.request(path, 'POST', headers=headers)
print "Token:",
print token
print "\n\n"
print type(json.loads(token)['token'])
print type(str(json.loads(token)['token']))
headers.update({'X-CSRF-Token': str(json.loads(token)['token'])})

login_body = {'username': 'qing', 'password': 'qing2drupal'} 
path = 'http://127.0.0.1:8080/drupal/rest/user/login.json'
response,qing_info= http.request(path, 'POST', body = json.dumps(login_body), headers=headers)
print response['set-cookie']
#print json.loads(qing_info)['name']
print json.loads(qing_info)
print "\n\n"

login_json = json.loads(token)
path = 'http://127.0.0.1:8080/drupal/rest/node/'
headers.update({'Cookie': response['set-cookie']})
print headers
response,token= http.request(path, 'POST', body = json.dumps({'title':'Title0'}), headers=headers)
print response 
print token

#path = 'http://127.0.0.1:8080/drupal/?q=node&destination=node'
#form_data =  {'name': 'qing', 'pass': 'qing2drupal', 'form_id':'user_login_block'}  
#headers = {'Content-type': 'application/x-www-form-urlencoded'}
#headers.update({'Connection': 'keep-alive'})   
#response, content = http.request(path, 'POST', headers=headers, body=urllib.urlencode(form_data))
#headers.update({'Cookie': response['set-cookie']})   ###将获得cookie设置到请求头中，以备下次请求使用
#print headers
#
##response, content = http.request(path, 'GET', headers=headers)  ##本次请求就不用带用户名，密码了
#
#  
###模拟登录  
##cj = cookielib.CookieJar()  
###用户名和密码
###form_data =  {'name': 'qing', 'pass': 'qing@drupal', 'op':'登录'}  
##form_data =  {'name': 'qing', 'pass': 'qing2drupal', 'form_id':'user_login_block'}  
##post_data = urllib.urlencode(form_data)
###登录路径  
##path = 'http://127.0.0.1:8080/drupal/?q=node&destination=node'
##opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  
##opener.addheaders = [('User-agent', 'Opera 9.2'), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
##('Accept-Language','zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3'),
##('Accept-Encoding','gzip, deflate'),
##('Connection','keep-alive')]
##urllib2.install_opener(opener) 
##req = urllib2.Request(path, post_data)  
##conn = urllib2.urlopen(req)
##
#article_data = {'title':'dddd', 
##'field_tags[und]':'tags', 
##'body[und][0][summary]':'',
##'body[und][0][value]':'body0',
##'body[und][0][format]':'filtered_html',
##'field_image[und][0][fid]':'0',
##'field_image[und][0][display]':'1',
##'changed':'',
#'form_build_id':'',
#'form_token':'',
#'form_id':'article_node_form',
#'menu[link_title]':'',
#'menu[description]':'',
#'menu[parent]':'main-menu:0',
#'menu[weight]':'0',
#'log':'',
#'path[pathauto]':'1',
#'comment':'2',
#'name':'qing',
#'date':'',
#'status':'1',
#'promote':'1',
#'additional_settings_active_tab':'edit-options',
#'op':'保存'
#}
#
#add_path = 'http://127.0.0.1:8080/drupal/node/add/article'
##add_path = 'http://127.0.0.1:8080/drupal/admin/people'
#response, content = http.request(add_path, 'GET', headers=headers)
##print headers
#soup = BeautifulSoup(content)
##for inputs in soup.findAll('input'):
##    print inputs
#form_token = soup.find('input',attrs={'name':'form_token'})['value']
#form_build_id = soup.find('input',attrs={'name':'form_build_id'})['value']
#article_data.update({'form_token':form_token})
#article_data.update({'form_build_id':form_build_id})
#
##add_path = 'http://127.0.0.1:8080/drupal/admin/people'
#files = {'files[field_image_und_0]':''}
#content_type, body = encode_multipart_formdata(article_data, files)
#article_data.update({'content-type': content_type})
#article_data.update({'content-length': str(len(body))})
#print article_data
##content = posturl(add_path, headers, article_data, files)
##response, content = http.request(add_path, 'POST', headers, body)
#response, content = http.request(add_path, 'POST', body,headers)
#print response
#print content
